<template>
  <div>
    <!-- 头部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索按钮 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="searchOrderName">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="orderList" stripe border>
        <el-table-column label="#" width="50">
          <template slot-scope="scope">{{
            scope.$index + (queryInfo.pagenum - 1) * queryInfo.pagesize + 1
          }}</template>
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格"
          width="100"
        ></el-table-column>
        <el-table-column label="是否付款" width="150">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.order_pay !== '1'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          width="100"
        ></el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | fmtYMDHMS }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="修改订单地址"
              placement="top"
              :enterable="false"
            >
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="showEditDialog(scope.row.order_id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="地址"
              placement="top"
              :enterable="false"
            >
              <el-button
                size="mini"
                type="success"
                icon="el-icon-location-outline"
                @click="showProgressBox"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :current-page="queryInfo.pagenum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
      ></el-pagination>
    </el-card>
    <el-dialog
      title="修改订单地址"
      :visible.sync="showOrderdialogVisible"
      width="50%"
      @close="closeOrderForm"
    >
      <el-form
        :model="orderForm"
        :rules="orderFormRules"
        ref="orderFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="orderForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="orderForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showOrderdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="showOrderdialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="物流进度"
      :visible.sync="showProgressBoxVisible"
      width="50%"
      @close="closeOrderForm"
    >
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showProgressBoxVisible = false">取 消</el-button>
        <el-button type="primary" @click="showProgressBoxVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'

export default {
  data() {
    return {
      // 订单名称，模糊查询
      searchOrderName: '',
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      orderList: [],
      showOrderdialogVisible: false,
      orderForm: {
        address1: [],
        address2: ''
      },
      orderFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'change' }
        ],
        address2: [
          { required: true, message: '详细地址不能空', trigger: 'blur' }
        ]
      },
      cityData,
      showProgressBoxVisible: false,
      progressInfo: []
    }
  },
  methods: {
    async showProgressBox() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200)
        return this.$message.error('获取快递信息失败')
      this.progressInfo = res.data
      console.log(this.progressInfo)

      this.showProgressBoxVisible = true
    },
    closeOrderForm() {
      this.$refs.orderFormRef.resetFields()
    },
    handleCurrentChange(newVal) {
      this.queryInfo.pagenum = newVal
      this.getOrderList()
    },
    handleSizeChange(newVal) {
      this.queryInfo.pagesize = newVal
      this.getOrderList()
    },
    // 根据订单id删除订单
    delOrder(Id) {},
    // 展示修改订单地址对话框
    async showEditDialog(Id) {
      // 查看订单详情
      const { data: res } = await this.$http.get(`orders/${Id}`)

      this.showOrderdialogVisible = true
    },
    // 获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取订单列表失败')
      this.orderList = res.data.goods
      this.total = res.data.total
      console.log(this.orderList)
    }
  },
  created() {
    this.getOrderList()
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
